package com.ccp.dev.core.basic.db.datasource.threadquery;

import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author zw
 * @2019/11/25 12:44
 * 文件说明:
 */
public class QueryThread implements Callable<Map<String,Object>> {


    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    private String querySQL;
    private  Map paraMap;
    private boolean isCount;

    private Map<String,Object> resultMap = new HashMap<String, Object>();

    public QueryThread(NamedParameterJdbcTemplate namedParameterJdbcTemplate, String querySQL, Map paraMap, boolean isCount) {
        super();
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
        this.querySQL = querySQL;
        this.paraMap = paraMap;
        this.isCount = isCount;
    }

    @Override
    public Map<String, Object> call(){
        if(isCount){
        	Integer total = namedParameterJdbcTemplate.queryForObject(querySQL, paraMap,Integer.class);
            resultMap.put("total",total);
        }else{
            List list = namedParameterJdbcTemplate.queryForList(querySQL, paraMap);
            resultMap.put("list",list);
        }
        return resultMap;
    }

    public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
        return namedParameterJdbcTemplate;
    }

    public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    public String getQuerySQL() {
        return querySQL;
    }

    public void setQuerySQL(String querySQL) {
        this.querySQL = querySQL;
    }

    public Map getParaMap() {
        return paraMap;
    }

    public void setParaMap(Map paraMap) {
        this.paraMap = paraMap;
    }

    public boolean isCount() {
        return isCount;
    }

    public void setCount(boolean count) {
        isCount = count;
    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }

    public void setResultMap(Map<String, Object> resultMap) {
        this.resultMap = resultMap;
    }
}
